<?php

namespace App\Services\Oauth;

use App\Exceptions\Response\Service\ServiceCode;
use App\Exceptions\Response\Service\ServiceException;
use App\Core\Utils\TwHelper\CommFunction;
use App\Models\Oauth\UserLoginOauthModel;
use App\Models\Oauth\UserModel;
use Illuminate\Support\Facades\Cookie;

/**
 * 第三方开放登陆服务
 */
class OauthorService
{
    const OAUTHER_OPEN_Id_KEY = 'oauther_open_id_key';

    public function getLoginOauth($type, $openId, $userId)
    {
        $where['type'] = $type;
        if ($openId) {
            $where['open_id'] = $openId;
        }
        if ($userId) {
            $where['user_id'] = $userId;
        }
        $where['status'] = UserLoginOauthModel::STATUS_NOMAL;
        return UserLoginOauthModel::getLoginOauth($where);
    }

    /**
     * 绑定第3方帐号
     */
    public function addLoginOauth($userId)
    {
        //cookie里面获取第3方帐号信息
        $oauthorOpenInfo = Cookie::get($this->getOautherOpenIdKey(), '');
        if ($oauthorOpenInfo) {
            $openId = $oauthorOpenInfo['opendId'];
            $type = $oauthorOpenInfo['type'];
            $loginOauth = $this->getLoginOauth($type, '', $userId);
            if ($loginOauth) {
                throw new ServiceException(ServiceCode::SERVICE_OAUTH_BONDED, '', []);
            }
            return $this->addLoginOauthInfo($userId, $openId, $type);
        }
    }

    /**
     * 强制绑定第3方帐号
     */
    public function boundOauth()
    {
        $oauthorOpenInfo = Cookie::get($this->getOautherOpenIdKey(), '');
        $user = new User();
        $userId = $user->getUserId();
        if ($oauthorOpenInfo && $userId) {
            $openId = $oauthorOpenInfo['opendId'];
            $type = $oauthorOpenInfo['type'];
            //解除历史绑定
            $where['user_id'] = $userId;
            $where['type'] = $type;
            $data['status'] = UserLoginOauthModel::STATUS_BAN;
            if (UserLoginOauthModel::updateLoginOauth($where, $data)) {
                //强制绑定
                return $this->addLoginOauthInfo($userId, $openId, $type);
            } else {
                throw new ServiceException(ServiceCode::SERVICE_OAUTH_BONDED_FAIL, '', []);
            }
        } else {
            throw new ServiceException(ServiceCode::SERVICE_OAUTH_VALUE_NOT_EXIST, '', []);
        }
    }


    public function addLoginOauthInfo($userId, $openId, $type)
    {
        $data['user_id'] = $userId;
        $data['open_id'] = $openId;
        $data['type'] = $type;
        $data['created'] = time();
        if (UserLoginOauthModel::addLoginOauth($data)) {
            //清除cookie
            Cookie::queue(Cookie::forget($this->getOautherOpenIdKey()));
            unset($data);
            $data['status'] = 1;
            return $data;
        } else {
            throw new ServiceException(ServiceCode::SERVICE_OAUTH_BONDED_FAIL, '', []);
        }
    }

    public function setlogin($user)
    {
        if ($user) {
            //将用户信息传入给封装的用户类,设置登录信息
            $userService = new User($user);
        } else {
            throw new ServiceException(ServiceCode::SERVICE_OAUTH_USER_NOT_EXIST, '', []);
        }
    }

    public function getOautherOpenIdKey()
    {
        return md5(self::OAUTHER_OPEN_Id_KEY);
    }

    public function getOauther($where)
    {
        return UserLoginOauthModel::getLoginOauth($where);
    }
    public function updateLoginOauth($where, $data)
    {
        return UserLoginOauthModel::updateLoginOauth($where, $data);
    }

}